package com.bbs.toutiao.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;

/**
 * 描述: web配置类
 * 作者： zhangBin
 * 时间： 2023/9/17 11:52
 * 版本： 1.0
 */

@SpringBootConfiguration
public class WebConfig extends WebMvcConfigurationSupport {
    @Resource
    JWTInterceptor jwtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] paths = new String[]{
                "/api/loginUser",
                "/",
                "/api/register",
                "/api/email",
                "/api/client/**",
                "/api/article/articleSelectQuery",
                "/api/commentClient/queryCommentClient",
                "/api/article/articlesByUserId/**",
                "/article/findByIdArticle/**",
                "/sse/createSse",
                "/static/**",
                "/admin/home/onlineCount",
                "/api/article/userSearch/**",
                "/api/userHome/getUserHomeByUserId/**"
        };
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(paths)
                .order(1); // 设置拦截器的顺序
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //重写这个方法，映射静态资源文件
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/");

        super.addResourceHandlers(registry);
    }

}
